package com.xhtt.common.utils;

import com.alibaba.fastjson.JSON;
import com.xhtt.modules.sys.service.SysUserService;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.timeout.IdleStateEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yeauty.annotation.*;
import org.yeauty.pojo.ParameterMap;
import org.yeauty.pojo.Session;

import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;

@ServerEndpoint(value = "/position", prefix = "websocket")
@Component
public class MyWebSocket {
    @Autowired
    private SysUserService sysUserService;

    @OnOpen
    public void onOpen(Session session, HttpHeaders headers, ParameterMap parameterMap) throws IOException {
//        String paramValue = parameterMap.getParameter("paramKey");
//        System.out.println(paramValue);
        Constant.webSocketSessionQueue.add(session);
    }

    @OnClose
    public void onClose(Session session) throws IOException {
        Constant.webSocketSessionQueue.remove(session);
//        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        Constant.webSocketSessionQueue.remove(session);
        throwable.printStackTrace();
    }

    @OnMessage
    public void onMessage(Session session, String message) throws InterruptedException {
        if (null == session || !session.channel().isActive()) {
            Constant.webSocketSessionQueue.remove(session);
        } else {
            //再统计下这些人的分组
            int all = Constant.userPositionList.size();
            int car = 0;
            int pot = 0;
            if (all > 0) {
                Set<Integer> userIds = Constant.userPositionList.stream().map(up -> up.getUserId()).collect(Collectors.toSet());
                // 过滤出装车组人数量
                car = sysUserService.getZCZCount(userIds);
                pot = all - car;
            }
            session.sendText(JSON.toJSONString(R.ok().put(Constant.DATA, Constant.userPositionList).put("all", all).put("car", car).put("pot", pot)));
        }
    }

    @OnBinary
    public void onBinary(Session session, byte[] bytes) {
        for (byte b : bytes) {
//            System.out.println(b);
        }
        session.sendBinary(bytes);
    }

    @OnEvent
    public void onEvent(Session session, Object evt) {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            switch (idleStateEvent.state()) {
                case READER_IDLE:
                    System.out.println("read idle");
                    break;
                case WRITER_IDLE:
                    System.out.println("write idle");
                    break;
                case ALL_IDLE:
                    System.out.println("all idle");
                    break;
                default:
                    break;
            }
        }
    }

}
